//
//  CLSlideEffectWindow.m
//  CL1024
//
//  Created by liu nian on 13-3-25.
//  Copyright (c) 2013年 liunian. All rights reserved.
//

#import "CLSlideEffectWindow.h"
#import "CLAppDelegate.h"
#import <QuartzCore/QuartzCore.h>


#define kNeedRestoreWidth 80 
#define kNeedAdjustDx 6
#define kNeedAdjustAlpha 0.8




@interface CLSlideEffectWindow()
{
    CGPoint tStartLocation;
    float   m_fDXCount;
    BOOL    m_bNeedRestoreThisViewLayout;
    BOOL    m_bNeedPushNextView;
    BOOL    m_bSupportSlidingTurnPage;
    
    BOOL    m_bAlreadySlidingDirection;
    BOOL    m_bLeftSliding;
    BOOL    m_bRightSliding;
    
    NSTimer *m_popOrPushViewTimer;
    UIView  *m_shadowView;
    
    UIView *inwindowview;
}


@end

@implementation CLSlideEffectWindow

@synthesize delegate;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        m_fDXCount = 0;
        

        
        //添加阴影层
        m_shadowView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        m_shadowView.backgroundColor = [UIColor blackColor];
        [self addSubview:m_shadowView];

        
        //添加手势
        UIPanGestureRecognizer  *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handelPan:)];
        [gesture setMaximumNumberOfTouches:1];
        [gesture setMinimumNumberOfTouches:1];
        gesture.delegate = self;
        [self addGestureRecognizer:gesture];
        [gesture release];
    }
    return self;
}



-(void)handelPan:(UIPanGestureRecognizer*)recognizer
{
     inwindowview = self.rootViewController.view;
//     CGPoint offset = [recognizer locationOfTouch:0 inView:self];
    
//    CGRect rcView = inwindowview.frame;
//     rcView.origin.x += offset.x;
//    [inwindowview setFrame:rcView];
    if (recognizer.state == UIGestureRecognizerStateChanged) {
        //注意，这里取得的参照坐标系是该对象的上层View的坐标。
        CGPoint offset = [recognizer translationInView:self];
        //通过计算偏移量来设定draggableObj的新坐标
        
        CGRect rcview = inwindowview.frame;
        rcview.origin.x += offset.x;
        rcview.origin.x = rcview.origin.x >= [[UIScreen mainScreen] bounds].size.width ? [[UIScreen mainScreen] bounds].size.width : rcview.origin.x;
        CGFloat currentX = rcview.origin.x;
        //禁止划出左边屏幕边缘
        if (currentX < 0) {
            return;
        }
        
        if(currentX < kNeedRestoreWidth){ //not pop
            m_bNeedRestoreThisViewLayout = YES;
        }
        else{ //pop
            m_bNeedRestoreThisViewLayout = NO;
        }
        
        [inwindowview setFrame:rcview];
        [self changeViewFrameAndAlpha:inwindowview];
        [recognizer setTranslation:CGPointMake(0, 0) inView:self];
    }

    if (recognizer.state == UIGestureRecognizerStateEnded) {
        [self restoreViewRect];
    }
}

- (void)restoreViewRect
{
     inwindowview = self.rootViewController.view;
    if(m_bNeedRestoreThisViewLayout){
        [self popOrPushView:inwindowview xOrigin:0];
    }
    else{
        // pop this view
        [self popOrPushView:inwindowview xOrigin:[[UIScreen mainScreen] bounds].size.width];
    }
//    m_fDXCount = 0;
}

- (void)popOrPushView:(UIView *)subView xOrigin:(NSInteger)xPos
{
    if(subView != nil){
        
        CGRect rcView = subView.frame;
        rcView.origin.x = xPos;
        [UIView animateWithDuration:0.3 animations:^{
            [subView setFrame:rcView];
            [self changeViewFrameAndAlpha:subView];
        } completion:^(BOOL finished) {

            if (!m_bNeedRestoreThisViewLayout) {
                
                //收起视图的处理操作
                UIViewController *vc = self.rootViewController;
                if ([vc respondsToSelector:@selector(slideOver)]) {
                    [vc slideOver];
                }
                
                //将windows窗口初始位置至于右边
                CGRect selfFrame = self.frame;
                selfFrame.origin.x = selfFrame.size.width;
                self.frame = selfFrame;
                
                self.rootViewController = nil;
                
                //取消当前的使用权，重置上一个window视图
                [self.prevWindow makeKeyAndVisible];
                //注销通知观察者
                [[NSNotificationCenter defaultCenter] removeObserver:self];
                m_bNeedRestoreThisViewLayout = YES;
            }
        }];
    }
}



- (void)changeViewFrameAndAlpha:(UIView *)currentSubView
{
    float proportion = [[UIScreen mainScreen] bounds].size.width - currentSubView.frame.origin.x;
    float ratio = proportion / ([[UIScreen mainScreen] bounds].size.width);
    float alpha = kNeedAdjustAlpha * ratio;
    m_shadowView.alpha = alpha;
}

-(void)colseThisWindow
{
    inwindowview = self.rootViewController.view;
    m_shadowView.alpha = 1.0;
    m_bNeedRestoreThisViewLayout = NO;
    [self popOrPushView:inwindowview xOrigin:[[UIScreen mainScreen] bounds].size.width]; 
}


-(void)makeKeyAndVisible
{
    [super makeKeyAndVisible];
    NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
    [notifCenter addObserver:self selector:@selector(colseThisWindow) name:CL_closeWindows_NotifiationName object:nil];
}


-(void)dealloc
{
    [super dealloc];
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
